﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.Clustering
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FeatureSetEditor : Form, IComponentEditor
    {
        private object _ActivityNode;
        private FeatureSetComponent _FeatureSetComponent;

        /// <summary>
        /// 
        /// </summary>
        public FeatureSetEditor()
        {
            InitializeComponent();
        }

        #region IComponentEditor Members

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof (FeatureSetComponent); }
        }

        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._FeatureSetComponent = (FeatureSetComponent) wfComponent;

            this.txt_DocTermListFilePath.Text = this._FeatureSetComponent.DocumentTermListFilePath;
            this.txt_StartingClusterResultFilePath.Text = this._FeatureSetComponent.StartingClusterResultFilePath;
            this.txt_TermWeightTrackingFileName.Text = Path.GetFileName(this._FeatureSetComponent.TermWeightFilePath);
            this.txt_IterationSummaryFileName.Text = Path.GetFileName(this._FeatureSetComponent.IterationFilePath);
            this.txt_ClusterResultFileNamePattern.Text =
                Path.GetFileName(this._FeatureSetComponent.ClusterResultFileNamePattern);
        }

        public void SaveSettings()
        {
            if(!string.IsNullOrEmpty(this.txt_StartingClusterResultFilePath.Text))
            {
                string folderPath = Path.GetDirectoryName(this.txt_StartingClusterResultFilePath.Text);
                this._FeatureSetComponent.DocumentTermListFilePath = this.txt_DocTermListFilePath.Text;
                this._FeatureSetComponent.StartingClusterResultFilePath = this.txt_StartingClusterResultFilePath.Text;
                this._FeatureSetComponent.TermWeightFilePath = Path.Combine(folderPath,
                                                                            this.txt_TermWeightTrackingFileName.Text);
                this._FeatureSetComponent.IterationFilePath = Path.Combine(folderPath,
                                                                           this.txt_IterationSummaryFileName.Text);
                this._FeatureSetComponent.ClusterResultFileNamePattern = Path.Combine(folderPath,
                                                                                      this.
                                                                                          txt_ClusterResultFileNamePattern
                                                                                          .Text);
            }
            else
            {
                MessageBox.Show("Starting cluster file must be specified");
                this.txt_StartingClusterResultFilePath.Focus();
            }
        }

        public TestResult Test()
        {
            throw new NotImplementedException();
        }

        #endregion

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent !=null)
            {
                this.UpdateComponentSettingEvent(this._ActivityNode, this._FeatureSetComponent);
            }
            this.Close();
        }

        private void btn_BrowseTermList_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg=new OpenFileDialog();
            dlg.Filter = "Term list files(*.xml)|*.xml|All files(*.*)|*.*";
            if(dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                this.txt_DocTermListFilePath.Text = dlg.FileName;
            }
        }

        private void btn_BrowseInitialClusterResult_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Cluster result files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.txt_StartingClusterResultFilePath.Text = dlg.FileName;
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
